//
//  StarHttpTool.m
//  MoreStarLiveTelecast
//
//  Created by babbage on 16/8/19.
//  Copyright © 2016年 babbage. All rights reserved.
//

#import "StarHttpTool.h"
#import "AFNetworking.h"
#import "QiniuUploadHelper.h"//封装的七牛上传图片


@implementation StarHttpTool

#pragma mark - get -
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(responseBlock)successHandler failure:(requestFailureBlock)failureHandler
{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    [paramsM setObject:[GJCFStringToMD5(@"acyw") lowercaseString] forKey:@"token"];
    
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr GET:[BaseUrl stringByAppendingString:url] parameters:paramsM progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager:mgr];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);}];
    
}

#pragma mark - post -
+ (void)post:(NSString *)url params:(NSDictionary *)params success:(responseBlock)successHandler failure:(requestFailureBlock)failureHandler
{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    [paramsM setObject:[GJCFStringToMD5(@"acyw") lowercaseString] forKey:@"token"];
    
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr POST:[BaseUrl stringByAppendingString:url] parameters:paramsM progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager: mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);}];
}

#pragma mark - put -
+ (void)put:(NSString *)url params:(NSDictionary *)params success:(responseBlock)successHandler failure:(requestFailureBlock)failureHandler
{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    [paramsM setObject:[GJCFStringToMD5(@"acyw") lowercaseString] forKey:@"token"];
    
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr PUT:[BaseUrl stringByAppendingString:url] parameters:paramsM success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager: mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);}];

}

#pragma mark - delete-
+ (void)deleteRequest:(NSString *)url params:(NSDictionary *)params success:(responseBlock)successHandler failure:(requestFailureBlock)failureHandler;
{
    NSMutableDictionary *paramsM = [NSMutableDictionary dictionaryWithDictionary:params];
    [paramsM setObject:[GJCFStringToMD5(@"acyw") lowercaseString] forKey:@"token"];
    
    AFHTTPSessionManager *mgr = [self getRequstManager];
    [mgr DELETE:[BaseUrl stringByAppendingString:url] parameters:paramsM success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager:mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);}];
}

#pragma mark - post 上传图片-
+(void)uploadAvatarImgWithUrl:(NSString *)url WithImg:(UIImage *)avatarImg fileName:(NSString *)fileName success:(responseBlock)successHandler failure:(requestFailureBlock)failureHandler
{
    AFHTTPSessionManager *mgr = [self getRequstManager];
    NSData *imgData = UIImageJPEGRepresentation(avatarImg, 0.5);
    [mgr POST:[BaseUrl stringByAppendingString:url] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imgData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self prettyPrintAboutHttpWithOperation:task withResponseObj:responseObject withManager:mgr];
        !successHandler?:successHandler(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {!failureHandler?:failureHandler(error);}];
}

//上传到七牛服务器
+(void)uploadToQiniuWithImageArray:(NSArray *)array success:(responseBlock)successHandler failure:(requestFailureBlock)failureHandler
{
    [QiniuUploadHelper uploadImages:array progress:^(CGFloat progress) {
    } success:^(NSArray *arr) {!successHandler?:successHandler(arr);
    } failure:^{MJRefreshLog(@"七牛上传图片失败");
    }];
}

#pragma mark- AFHTTPRequestOperationManager初始化 网络超时设置 请求头
+(AFHTTPSessionManager *)getRequstManager
{
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    //如果报接受类型不一致请替换一致text/html
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    // 设置token
//    [mgr.requestSerializer setValue:[GJCFStringToMD5(@"acyw") lowercaseString] forHTTPHeaderField:@"token"];
    
    // 请求超时设定
     mgr.requestSerializer.timeoutInterval = 10;
    return mgr;
}

#pragma mark- 打印url 和 json数据 httpHeader用于调试
+(void)prettyPrintAboutHttpWithOperation:(NSURLSessionDataTask *)operation withResponseObj:(id)responseObj withManager:(AFHTTPSessionManager *)mgr
{
#if DEBUG
    // 请求的url
     NSLog(@"fianlUrl: %@", operation.response.URL.absoluteString);
    
    // 请求的json数据打印
     NSData *dataJson = [NSJSONSerialization dataWithJSONObject:responseObj options:NSJSONWritingPrettyPrinted error:nil];
     NSString *jsonStr = [[NSString alloc] initWithData:dataJson encoding:NSUTF8StringEncoding];
     NSLog(@"dataJson: %@", jsonStr);
    
    // 请求的header信息
     NSDictionary *dict = mgr.requestSerializer.HTTPRequestHeaders;
     NSLog(@"请求头httpHeader: %@", dict);
#endif
}


@end
